'''
Description: 
Version: 2.0
Autor: zyw
Date: 2021-12-31 11:15:01
LastEditors: zyw
LastEditTime: 2022-03-09 11:36:17
'''
from selenium.webdriver import ChromeOptions
import pytest
from selenium import webdriver
import os,sys
from page.loginPage import LoginPage
base_path = os.path.abspath(os.path.join(os.getcwd(), "."))
sys.path.append(base_path)
from common.hand_path import driver_path
from common.hand_conf import conf


def create_driver(is_headers = True):
    """
    创建driver：默认启动浏览器窗口,is_headers设为Flase则以无头模式运行
    :param is_headers: True or False
    """
    if is_headers:
        driver = webdriver.Chrome(os.path.join(driver_path,'chromedriver.exe'))
    else:
        # 设置无头浏览器模式
        chrome_options = ChromeOptions()
        # 无头模式
        chrome_options.add_argument('--headless')
        # 指定窗口大小(无头模式下maximize_window()方法无效，需要通过下面的参数指定大小)
        chrome_options.add_argument('--window-size=1920,1080')
        # 禁用GPU
        chrome_options.add_argument('--disable-gpu')
        # 非沙箱环境
        chrome_options.add_argument('--no-sandbox')
        driver = webdriver.Chrome(executable_path=os.path.join(driver_path,'chromedriver.exe'),options=chrome_options)
    return driver


@pytest.fixture(scope='function')
def login_fixture():
    # 用例前置操作
    driver = create_driver(is_headers= False)
    driver.implicitly_wait(10)
    driver.maximize_window()
    driver.get('https://test.lab17025.cn')
    yield driver
    # 用例后置操作
    driver.quit()
    
@pytest.fixture(scope='function')    
def admin_login():
    """管理员登录前置操作"""
    driver = webdriver.Edge(os.path.join(driver_path,'msedgedriver.exe'))
    driver.implicitly_wait(10)
    driver.maximize_window()
    driver.get(conf.get('env','base_url'))
    login_page = LoginPage(driver)
    login_page.login(username=conf.get('test_data','admin_user'),password=conf.get('test_data','password'))
    yield driver
    # 用例后置操作
    driver.quit()
    
@pytest.fixture(scope='function')    
def manager_login():
    """管理员登录前置操作"""
    driver = webdriver.Edge(os.path.join(driver_path,'msedgedriver.exe'))
    driver.implicitly_wait(10)
    driver.maximize_window()
    driver.get(conf.get('env','base_url'))
    login_page = LoginPage(driver)
    login_page.login(username=conf.get('test_data','manager_user'),password=conf.get('test_data','password'))
    yield driver
    # 用例后置操作
    driver.quit()
    
@pytest.fixture(scope='function')    
def engineer_login():
    """管理员登录前置操作"""
    driver = webdriver.Edge(os.path.join(driver_path,'msedgedriver.exe'))
    driver.implicitly_wait(10)
    driver.maximize_window()
    driver.get(conf.get('env','base_url'))
    login_page = LoginPage(driver)
    login_page.login(username=conf.get('test_data','admin_user'),password=conf.get('test_data','password'))
    yield driver
    # 用例后置操作
    driver.quit()